<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>公告详情-家电维修系统</title>
    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    <link th:href="@{/css/all.min.css}" rel="stylesheet">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="/">家电维修系统</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/orders">我的订单</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/profile">个人资料</a>
                    </li>
                    <li class="nav-item" active>
                        <a class="nav-link" href="/announcements">公告</a>
                    </li>
                </ul>
                <div class="d-flex">
                    <button class="btn btn-outline-light" onclick="logout()">退出</button>
                </div>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="row">
            <div class="col-md-8 mx-auto">
                <div class="card">
                    <div class="card-body">
                        <h2 class="card-title text-center mb-4" id="title"></h2>
                        <div class="text-muted text-center mb-4">
                            <small>发布时间：<span id="createdAt"></span></small>
                            <small class="ms-3">浏览次数：<span id="viewCount"></span></small>
                        </div>
                        <div class="ql-snow">
                            <div class="ql-editor" id="content">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 引入必要的依赖 -->
    <script th:src="@{/js/jquery.min.js}"></script>
    <script th:src="@{/js/bootstrap.bundle.min.js}"></script>
    <script th:src="@{/js/quill.min.js}"></script>
    <link th:href="@{/css/quill.snow.css}" rel="stylesheet">
    <script src="/js/request.js"></script>
    <script>
        // 获取公告详情
        window.loadAnnouncementDetail = function() {
            const id = new URLSearchParams(window.location.search).get('id');
            if (!id) {
                window.location.href = '/';
                return;
            }

            $.ajax({
                url: '/api/announcements/' + id,
                type: 'GET',
                headers: {
                    'Authorization': 'Bearer ' + localStorage.getItem('token')
                },
                success: function(response) {
                    const createdAt = response.createdAt;
                    const views = response.views || 0;
                    
                    $('#title').text(response.title);
                    $('#createdAt').text(createdAt ? new Date(createdAt).toLocaleString() : '未发布');
                    $('#viewCount').text(views);
                    $('#content').html(response.content);
                },
                error: function(xhr) {
                    if (xhr.status === 401) {
                        window.location.href = '/login';
                    } else {
                        console.error('获取公告详情失败');
                        window.location.href = '/';
                    }
                }
            });
        };

        // 退出登录
        window.logout = function() {
            localStorage.removeItem('token');
            window.location.href = '/login';
        };

        // 页面加载完成后初始化
        $(document).ready(function() {
            if (!localStorage.getItem('token')) {
                window.location.href = '/login';
                return;
            }
            window.loadAnnouncementDetail();
        });
    </script>
</body>
</html> 